﻿using System.Linq;

using Common.Base;
using Common.Enums;

using GameCommon.Base;
using GameCommon.HitBoxes;
using GameCommon.Manager;

using GameGraphics.Components;
using GameGraphics.Enums;

using GameNetwork;
using GameNetwork.Interfaces;

using Microsoft.Xna.Framework;

using GameComponent = GameCommon.Components.GameComponent;
using GlobalService = GameCommon.Services.Service;

namespace GameGraphics.Screens.HUD
{
    public class Hud : ScreenBase
    {
        private IGameNetwork network;

        private Texture bar;

        private Text goldText;

        private Text playerText;

        private Texture bottomBar;

        private GameModeIcon farmerIcon;

        private GameModeIcon fighterIcon;

        private Texture barrackIcon;

        private Texture goldMineIcon;

        private MessageBox messageBox;

        public Hud(IScreenChanger changer, object argument)
            : base(changer, argument)
        {
            this.Layer = 3;
        }

        public event HudCommandEventHandler OnGameModeChanged;

        public override ScreenEnum Screen
        {
            get
            {
                return ScreenEnum.Hud;
            }
        }

        public override void Initialize()
        {
            base.Initialize();

            this.network = NetworkDistributor.GetGameNetwork();

            this.AddCommand("FarmerMode", this.OnFarmerModeClicked);
            this.AddCommand("FighterMode", this.OnFighterModeClicked);
            this.AddCommand("BuildGoldMine", this.OnBuildGoldMine);
            this.AddCommand("BuildBarrack", this.OnBuildBarrack);
           
            this.EvaluateArguments();
        }

        public override void LoadContent()
        {
            base.LoadContent();

            this.BuildHead();

            this.BuildBottom();

            this.messageBox = new MessageBox("MB_Messages", FontSizeEnum.S20, FontEnum.Visitor, 2);
            this.messageBox.Dimension = new Dimension(10, 30, 450, 50);
            this.messageBox.HitBox = new RectangleHitBox("RHB_MessageBox", this.messageBox.Dimension, "MessageBox");

            this.Components.Add(this.messageBox);
            this.farmerIcon.Select();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            this.messageBox.Update();
            this.goldText.Value = this.network.Player.GoldAmount.ToString();
        }
        
        public void AddMessage(Result result)
        {
            this.messageBox.Add(result);
        }
        
        private void BuildBottom()
        {
            var container = new Container("HUD_C_Hud");
            container.Dimension = Managers.ContentManager.GetViewportSize();

            var size = new Size(Managers.ContentManager.GetViewportWidth(), 50);
            this.bottomBar = new Texture("HUD_T_Bar", Managers.ContentManager.GetRectangle(Color.Black), container);
            this.bottomBar.Dimension = new Dimension(0, 0, size);
            this.bottomBar.VerticalAlignment = VerticalAlignment.Bottom;

            this.farmerIcon = new GameModeIcon("HUD_GMI_Farmer", Managers.ContentManager.GetHudTexture2D("Farmer"), this.bottomBar);
            this.farmerIcon.VerticalAlignment = VerticalAlignment.Middle;
            this.farmerIcon.Offset = new Offset(0, 0, 10, 0);
            this.farmerIcon.HitBox = new RectangleHitBox("HUD_RHT_Farmer", this.farmerIcon.Dimension, "FarmerMode");

            this.fighterIcon = new GameModeIcon("HUD_GMI_Fighter", Managers.ContentManager.GetHudTexture2D("Fighter"), this.bottomBar);
            this.fighterIcon.VerticalAlignment = VerticalAlignment.Middle;
            this.fighterIcon.Offset = new Offset(0, 0, 40, 0);
            this.fighterIcon.HitBox = new RectangleHitBox("HUD_RHT_Fighter", this.fighterIcon.Dimension, "FighterMode");

            this.barrackIcon = new Texture("HUD_T_BarrackIcon", Managers.ContentManager.GetHudTexture2D("Barrack"), this.bottomBar);
            this.barrackIcon.VerticalAlignment = VerticalAlignment.Middle;
            this.barrackIcon.Offset = new Offset(0, 0, 100, 0);
            this.barrackIcon.HitBox = new RectangleHitBox("HUD_RHT_Barrack", this.barrackIcon.Dimension, "BuildBarrack");

            this.goldMineIcon = new Texture("HUD_T_GoldMineIcon", Managers.ContentManager.GetHudTexture2D("GoldMine"), this.bottomBar);
            this.goldMineIcon.VerticalAlignment = VerticalAlignment.Middle;
            this.goldMineIcon.Offset = new Offset(0, 0, 160, 0);
            this.goldMineIcon.HitBox = new RectangleHitBox("HUD_RHT_GoldMine", this.goldMineIcon.Dimension, "BuildGoldMine");

            this.Components.Add(this.bottomBar);
            this.Components.Add(this.farmerIcon);
            this.Components.Add(this.fighterIcon);
            this.Components.Add(this.barrackIcon);
            this.Components.Add(this.goldMineIcon);
        }

        private void BuildHead()
        {
            var size = new Size(Managers.ContentManager.GetViewportWidth(), 20);
            this.bar = new Texture("HUD_T_Bar", Managers.ContentManager.GetRectangle(Color.Black));
            this.bar.Dimension = new Dimension(0, 0, size);
            this.bar.Layer = 50;

            this.goldText = new Text("HUD_T_Gold", FontSizeEnum.S20, FontEnum.Visitor, this.bar);
            this.goldText.Value = this.network.Player.GoldAmount.ToString();
            this.goldText.Layer = 51;
            this.goldText.HorizontalAlignment = HorizontalAlignment.Right;
            this.goldText.VerticalAlignment = VerticalAlignment.Middle;
            this.goldText.Offset = new Offset(0, 10, 0, 0);

            this.playerText = new Text("T_Player", FontSizeEnum.S20, FontEnum.Visitor, this.bar);
            this.playerText.Value = this.network.Player.Name + " vs " + this.network.Opponents.First().Name;
            this.playerText.Layer = 51;
            this.playerText.HorizontalAlignment = HorizontalAlignment.Left;
            this.playerText.VerticalAlignment = VerticalAlignment.Middle;
            this.playerText.Offset = new Offset(0, 0, 10, 0);

            this.Components.Add(this.bar);
            this.Components.Add(this.goldText);
            this.Components.Add(this.playerText);
        }

        private void EvaluateArguments()
        {
            var command = Argument as HudCommandEventHandler;
            if (command == null)
            {
                return;
            }

            this.OnGameModeChanged = command;
        }
        
        private void OnBuildBarrack(GameComponent obj)
        {
            this.InvokeHudCommand(HudCommandEnum.BuildBarrack);
        }

        private void OnBuildGoldMine(GameComponent obj)
        {
            this.InvokeHudCommand(HudCommandEnum.BuildGoldMine);
        }

        private void OnFighterModeClicked(GameComponent obj)
        {
            this.fighterIcon.Select();
            this.farmerIcon.Deselect();

            this.InvokeHudCommand(HudCommandEnum.SwitchedToFighterMode);
        }

        private void OnFarmerModeClicked(GameComponent obj)
        {
            this.fighterIcon.Deselect();
            this.farmerIcon.Select();

            this.InvokeHudCommand(HudCommandEnum.SwitchedToFarmerMode);
        }

        private void InvokeHudCommand(HudCommandEnum command)
        {
            if (this.OnGameModeChanged != null)
            {
                this.OnGameModeChanged(command);
            }
        }
    }
}